﻿using System;
using System.Collections.Generic;
using System.Text;
using AudioPlayer.Plugin.Tags;
using AudioPlayer.Plugin.Storage;
using TagLib;
namespace AudioPlayer.Plugin.TagLibrary {
	public class Provider : ITagProvider,IPluginHost {
		#region Fields
		private IHost _host;
		#endregion
		#region Private Voids
		private void FillData(File file,ISong song) {
			song.Filename = file.Name;
			song.Artist = file.Tag.FirstPerformer;
			song.Album = file.Tag.Album;
			song.Genre = file.Tag.FirstGenre;
			song.Title = file.Tag.Title;
			song.Year = (int)file.Tag.Year;
			song.Length = (int)file.Properties.Duration.TotalSeconds;
			if (file.Tag.Title == null) {
				string[] split = file.Name.Split(new char[] { '\\' });
				song.Title = split[split.Length - 1];
			}
		}
		#endregion
		#region ITagProvider Members

		public IList<string> Formats {
			get {
				return new List<String>(new string[] { ".mp3", ".wav", ".ogg" });
			}
		}

		public IList<string> Mimetypes {
			get { return new List<string>(); }
		}

		public ISong Read(string url) {
			if (System.IO.File.Exists(url))
			{
				try
				{
					File file = File.Create(url);
					ISong s = _host.CreateSong();
					this.FillData(file, s);
					return s;
				}
				catch
				{
					return null;
				}
			}
			return null;
		}

		public void Save(ISong song) {
			
		}

		#endregion
		#region IDisposable Members

		public void Dispose() {
			
		}

		#endregion

		#region IPluginHost Members

		/// <summary>
		/// Gets or sets the host.
		/// After plugin loaded, the host will be set automaticaly.
		/// </summary>
		/// <value>The Host.</value>
		public IHost Host {
			get {
				return _host;
			}
			set {
				_host = value;
			}
		}

		#endregion
	}
}
